'use strict';
app.controller('agentAddCtrl', ['$scope', '$modalInstance', '$state', 'toaster', 'agentService',
    function($scope, $modalInstance, $state, toaster, agentService) {

        $scope.agent = {
            name: '',
            idCard: '',
            gender: 0,
            mobilePhone: '',
            address: '',
            email: ''
        };

        // 关键字搜索下拉代理区域列表
        $scope.uiSelectList = [];
        $scope.refreshSelectList = function (keyword) {
            if(keyword.length <= 0) {
                $scope.uiSelectList = [];
                if($scope.agent.selected != undefined) {
                    $scope.uiSelectList[0] = $scope.agent.selected;
                }
                return;
            }
            agentService.initAgentAreaSelect($scope, keyword).then(function(result){
                switch (result.status) {
                    case 200:
                        $scope.uiSelectList = result.data.data;
                        break;
                    default :
                        toaster.pop('error', "搜索失败", "搜索关键字代理区域下拉列表失败");
                        break;
                }
            });
        }

        // 选中代理区域后触发
        $scope.edChange = function () {
            agentService.isTheAreaAvailable($scope, $scope.agent.selected.id, "").then(function(result){
                switch (result.status) {
                    case 200:
                        switch (result.data.status){
                            case 1002:
                                toaster.pop('warning', "代理区域被占用", result.data.message);
                                $scope.agent.selected = undefined;
                                break;
                        }
                        break;
                    default :
                        toaster.pop('error', "检测区域可用失败", "检测所选的代理区域是否有代理商占用失败！");
                        $scope.agent.selected = undefined;
                        break;
                }
            });
        }

        $scope.AllowToAdd = true;
        $scope.add = function(form){
            if(!form.$valid) {
                // 表单验证没通过
                if($scope.agent.selected == undefined) {
                    toaster.pop('warning', "提示", "请选择代理区域");
                    $scope.AllowToAdd = true;
                    return;
                }
                return;
            }
            if(!$scope.AllowToAdd){
                return;
            }
            $scope.AllowToAdd=false;

            switch ($scope.agent.gender) {
                case 0:
                case 1:
                    break;
                default:
                    toaster.pop('warning', "提示", "请选择性别");
                    $scope.AllowToAdd = true;
                    return;
            }

            agentService.add($scope).then(function(result){
                switch (result.status) {
                    case 200:
                        $scope.cancel();
                        $state.reload('app.agent');
                        break;
                    case 1002:
                        $scope.agent.selected = undefined;
                    default :
                        toaster.pop('warning', "新增失败", result.message);
                        break;
                }
                $scope.AllowToAdd=true;
            });
        }

        $scope.cancel = function () {
            $modalInstance.dismiss('close');
        };
}]);